﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mygisdzy
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            axTOCControl1.SetBuddyControl(axMapControl1);
        }

        private void 打开shpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog OpFileDialog = new OpenFileDialog();//0penFileDialog:打开文件对话框
            OpFileDialog.Title = "添加矢里数据";
            OpFileDialog.Filter = "(*.shp) | *,shp";//filter:过滤器:"(*.shp) | *.shp”:筛掉其他数据，只留下shp数据
            if (OpFileDialog.ShowDialog() == DialogResult.OK)
            {
                string fullpath = OpFileDialog.FileName;
                string path = fullpath.Substring(0, fullpath.LastIndexOf("\\"));
                //从路径ful1path中检索子字符串,从开始截" +
                string name = fullpath.Substring(fullpath.LastIndexOf("\\") + 1);//从最后一个“\”的后一个开始，到最后(
                axMapControl1.AddShapeFile(path, name);//AddShapeFile:加载shp文件的工具
                axMapControl1.Refresh();//别新一下界面
            }
        }

        private void 打开mxdToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog OpFileDialog = new OpenFileDialog();//0penFileDialog:打开文件对话框
            OpFileDialog.Title = "添加msd数据";
            OpFileDialog.Filter = "(*.mxd) | *,mxd";//filter:过滤器:"(*.shp) | *.shp”:筛掉其他数据，只留下shp数据
            if (OpFileDialog.ShowDialog() == DialogResult.OK)
            {
                axMapControl1.LoadMxFile(OpFileDialog.FileName);
            }
        }

        private void 保存mxdToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string path = axMapControl1.DocumentFilename;
            IMxdContents pMxdContents = axMapControl1.Map as IMxdContents;
            IMapDocument pMapDocument = new MapDocumentClass();
            if (File.Exists(path))
            {
                pMapDocument.Open(path, "");
                pMapDocument.ReplaceContents(pMxdContents);
                pMapDocument.Save(true, true);
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 地图放大ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axMapControl1.CurrentTool = null;
            IEnvelope pEnvelope = axMapControl1.Extent;
            pEnvelope.Expand(0.5, 0.5, true);
            axMapControl1.Extent = pEnvelope;
            axMapControl1.Refresh();

        }

        private void 地图缩小ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axMapControl1.CurrentTool = null;
            IEnvelope pEnvelope = axMapControl1.Extent;
            pEnvelope.Expand(2, 2, true);
            axMapControl1.Extent = pEnvelope;
            axMapControl1.Refresh();

        }

        private void 全图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            axMapControl1.CurrentTool = null;
            ICommand command = new ControlsMapFullExtentCommand();
            command.OnCreate(axMapControl1.Object);
            command.OnClick();

        }

        private void 移动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ICommand command = new ControlsMapPanTool();
            command.OnCreate(axMapControl1.Object);
            axMapControl1.CurrentTool = command as ITool;

        }

        private void 缓冲区分析ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BufferForm bufferForm = new BufferForm(this.axMapControl1.Object);
            if (bufferForm.ShowDialog() == DialogResult.OK)
            {
                // 获取输出文件路径
                string strBufferPath = bufferForm.strOutputPath;
                // 缓冲区图层载入到MapControl
                int index = strBufferPath.LastIndexOf("\\");
                this.axMapControl1.AddShapeFile(strBufferPath.Substring(0, index),
                                          strBufferPath.Substring(index + 1));
            }
        }

        private void 叠置分析ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmClip clip = new frmClip();
            clip.CurrentMap = axMapControl1.Map;
            clip.Show();
        }
    }
}
